#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int com_int(const void* p1, const void* p2)
{
	if (*(int*)p1 > *(int*)p2)
		return 1;
	else if (*(int*)p1 < *(int*)p2)
		return -1;
	else
		return 0;
}
void test1()
{
	int arr[] = { 3,1,7,9,4,2,6,5,8,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), com_int);
	for (int i = 0; i < sz; i++) printf("%d ", arr[i]);
}
struct Stu {
	char name[20];
	int age;
};
//按照名字比较两个结构体数据
int com_stu_by_name(const void* p1, const void* p2)
{
	return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
}
//按照名字比较两个结构体数据
int com_stu_by_age(const void* p1, const void* p2)
{
	return ((struct Stu*)p1)->age - ((struct Stu*)p2)->age;
}
void test2()
{
	struct Stu arr[] = { {"zhangsan",20},{"lisi",35},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), com_stu_by_name);
	for (int i = 0; i < sz; i++) printf("%s ", arr[i].name);
	printf("\n");
}
void test3()
{
	struct Stu arr[] = { {"zhangsan",20},{"lisi",35},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), com_stu_by_age);
	for (int i = 0; i < sz; i++) printf("%s ", arr[i].name);
	printf("\n");
}
void Swap(char* p1, char* p2, size_t width)
{
	char tmp = 0;
	for (int i = 0; i < width; i++)
	{
		tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2++;
	}
}
void bubble_sort(void* base, size_t sz, size_t width, int (*cmp)(const void* p1, const void* p2))
{
	for (int i = 0; i < sz; i++)
	{
		for (int j = 0; j < sz - 1 - i; j++)
		{
			if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
			{
				Swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
			}
		}
	}
}
void test4()
{
	int arr[] = { 9,8,7,6,5,4,3,2,1,0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz, sizeof(arr[0]), com_int);
	for (int i = 0; i < sz; i++) printf("%d ", arr[i]);
}
void test5()
{
	struct Stu arr[] = { {"zhangsan",20},{"lisi",35},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), com_stu_by_name);
	for (int i = 0; i < sz; i++) printf("%s ", arr[i].name);
	printf("\n");
}
void test6()
{
	struct Stu arr[] = { {"zhangsan",20},{"lisi",35},{"wangwu",18} };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz, sizeof(arr[0]), com_stu_by_age);
	for (int i = 0; i < sz; i++) printf("%s ", arr[i].name);
	printf("\n");
}
int main()
{
	//test1();
	//test2();
	//test3();
	//test4();
	//test5();
	test6();
	return 0;
}